/**
 * @author   zhangyp
 * @created  2013-8-27 下午2:13:53
 * @email    zviolet@163.com
 * TODO      wps助手(用于打开wps程序)
 */
package com.bible.fire.api.wps;

import java.io.File;

import com.bible.fire.util.PackageUtil;
import com.bible.fire.util.TimeUtil;
import com.bible.fire.util.ToastUtil;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class WPSAssistant {
	
    private static String[] supportFileTypes= {"doc","docx","xls","xlsx","pdf","txt","ppt","pptx"};
	
	public static boolean isWpsSupportFileType( String fileType ){
		for( String type : supportFileTypes ){
			if( type.equalsIgnoreCase( fileType ) ){
				return true;
			}
		}
		return false;
	}
	
	private static Intent  getWPSIntent( File file ){
		Intent intent = new Intent(); 
		String packageName = "cn.wps.moffice_eng";
		String activityName = "cn.wps.moffice.documentmanager.PreStartActivity";
		intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
	    //执行动作     
	    intent.setAction(Intent.ACTION_VIEW);
	    //类别
	    //intent.addCategory(Intent.CATEGORY_DEFAULT);
	    intent.setClassName(packageName, activityName );
	    //执行的数据类型     
	    intent.setData( Uri.fromFile(file) );
	    return intent;
	}
	
	public static void readFile(Context context, File file){
		if( file == null ){
			ToastUtil.showMsg(context, "文件不存在");
			return;
		}
		Intent intent = getWPSIntent( file );
		Bundle bundle = new Bundle();
		bundle.putString("OpenMode", "ReadOnly");
		intent.putExtras( bundle );
		PackageUtil.openFileByThirdApp(context, file, intent);
	}

	public static void openFile(Context context, File file, boolean isSendSaveBroad){
		if( file == null ){
			ToastUtil.showMsg(context, "文件不存在");
			return;
		}
		Intent intent = getWPSIntent( file );
	    
	    //intent.putExtra("SendSaveBroad", true);
	    //intent.putExtra("ThirdPackage", context.getPackageName());	    
	    if( isSendSaveBroad && !file.getName().toLowerCase().endsWith(".pdf") ){
	    	Bundle bundle = new Bundle();
	        bundle.putString("OpenMode", "Normal");
	        bundle.putBoolean("SendSaveBroad", true);
	        bundle.putString("ThirdPackage", context.getPackageName());
		    //intent.putExtra("SavePath", savePath);
		    //bundle.putString("SavePath", savePath);
	        intent.putExtras( bundle );
	    }	    
	    PackageUtil.openFileByThirdApp(context, file, intent);
	}
}
